package com.scrawler.util;

/**
*   类说明
*  
*   @Description    
*   @creator         tangkun
*   @create-time     2012-3-20
*   @revision        $Id
*/



import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.RandomAccessFile;
import java.util.ArrayList;
import java.util.List;

public abstract class FileUtil {
	
	/**
	 * 把字符串写到本地磁盘.
	 * @param path
	 * @param content
	 */
	
	public static void writeStringToDisk(String path,String content)
	{
		RandomAccessFile raf = null;
		try {
			raf = new RandomAccessFile(path,"rw");
			raf.write(content.getBytes());
		} catch (FileNotFoundException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			if(raf != null)
			{
				try {
					raf.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}
	
	public static String readFile(String fileName){
		BufferedReader buffer = null;
		StringBuffer sb = new StringBuffer();
		try {
			buffer = new BufferedReader(new InputStreamReader(new FileInputStream(new File(fileName))));
			String line = null;
			while((line=buffer.readLine())!=null){
				sb.append(line).append("\n");
				//sb.append(line);
			}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			if(buffer != null){
				try {
					buffer.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					System.err.print("close buffer error: "+e);
				}
			}
		}
		return sb.toString();
	}
	
	public static List<String> readFileLines(String fileName){
		BufferedReader buffer = null;
		List<String> lines = new ArrayList<String>();
		try {
			buffer = new BufferedReader(new InputStreamReader(new FileInputStream(new File(fileName))));
			String line = null;
			while((line=buffer.readLine())!=null){
				lines.add(line);
			}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			if(buffer != null){
				try {
					buffer.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					System.err.print("close buffer error: "+e);
				}
			}
		}
		return lines;
	}
}
